package org.alexisjago.footballmanager.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "team")
public class Team {
	
	@Column(name = "name")
	private String name;
	
	@OneToMany
	@JoinTable(name="assignment",
        joinColumns={ 
            @JoinColumn(name="teamId", referencedColumnName="id")
        },
        inverseJoinColumns = @JoinColumn(name="playerId", referencedColumnName="id"))
	private Set<Player> players;
	
	@Id  
    @GeneratedValue 
	private int ID;

	/**
	 * Create a team with the supplied name
	 * 
	 * @param name - Name of the team
	 */
	public Team(String name){
		this();
		this.name = name;
	}
	
	public Team(){
		players = new HashSet<Player>();
	}

	public boolean addPlayer(Player player){
		return players.add(player);
	}
	
	public boolean hasPlayer(Player player){
		return players.contains(player);
	}
	
	public int size(){
		return players.size();
	}
	
	@Override public boolean equals(Object obj){
		if(obj == null)
			return false;
		if(!( obj instanceof Team))
			return false;
		
		Team team = (Team) obj;
		return (name.equals(team.getName()) &&
				players.equals(team.getPlayers())
				);
			
	}

	@Override public int hashCode() {
        return name.hashCode() ^ players.hashCode();
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Player> getPlayers() {
		return players;
	}

	public void setPlayers(Set<Player> players) {
		this.players = players;
	}
	 
	public int getID() {
		return ID;
	}

	public void setID(int iD) {
		ID = iD;
	}
	
}